package com.zheng.damo01.homeWeark.作业七;

public class MyArrayList implements Touchable{
    private Object[] all=new Object[2];
    private int total;
    //添加
    public void add(Object element){
        if (total==all.length){
            Object[] obj=new Object[all.length*2];
            for (int i = 0; i < total; i++) {
                obj[i]=all[i];
            }
            all=obj;
        }
        all[total]=element;
        total++;
    }
    public void remove(int index){
        if (index<0||index>total){
            System.out.println("没有这个元素");
        }else {
            for (int i = index; i < total; i++) {
                if (i==all.length-1){
                    all[i]=null;
                }
                all[i]=all[i+1];
            }
            total--;
        }
    }
    public void set(int index,Object value){
        if (index<0||index>total){
            System.out.println("没有这个元素");
        }else {
            all[index]=value;
        }
    }
    public Object get(int index){
        if (index<0||index>total){
            System.out.println("没有这个元素");
            return null;
        }else {
            return all[index];
        }
    }

    public Object[] getAll() {
        return all;
    }

    public void setAll(Object[] all) {
        this.all = all;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    public Selector select() {
        return new MySelector();
    }
    class MySelector implements Selector{
        int cursor;

        @Override
        public boolean hasNext() {
            return cursor != total;
        }

        @Override
        public Object next() {
            return all[cursor++];
        }
    }
}
